import type { ProTableProps } from '@ant-design/pro-components';
import type { CSSProperties, ReactNode } from 'react';

interface CustomProTableConfig {
  searchGutter?: number;
  searchSpan?: number;
  /**
   * 如需覆盖全部样式，请传入一个空对象
   * 不传值（或传入了 null/undefined）默认会设置一个 padding
   */
  searchStyle?: CSSProperties;
  defaultPageSize?: number;
  toolbar?: {
    title?: string;
    actions?: ReactNode[];
  };
  headerTitle?: ReactNode[];
  searchText?: string;
  optionRender?: (config: unknown, props: unknown, dom: ReactNode[]) => ReactNode[];
}
const useProTableConfig = (config?: CustomProTableConfig) => {
  const {
    optionRender,
    toolbar,
    defaultPageSize,
    searchSpan,
    searchGutter,
    searchStyle,
    headerTitle,
    searchText,
  } = config ?? {};

  return {
    defaultSize: 'large',
    options: false,
    rowKey: 'id',
    headerTitle,
    search: {
      searchGutter: searchGutter ?? 20,
      labelWidth: 'auto',
      span: searchSpan ?? 6,
      collapsed: false,
      collapseRender: false,
      searchText: searchText ?? '查询',
      optionRender:
        optionRender ?? ((searchConfig, props, [resetDom, submitDom]) => [submitDom, resetDom]),
      style: searchStyle ?? { padding: '32px 24px' },
    },
    pagination: {
      defaultPageSize: defaultPageSize ?? 10,
      showSizeChanger: true,
    },
    toolbar,
  } as ProTableProps<any, any>;
};

export default useProTableConfig;
